package sesi.ejava.stream;

import android.os.Handler;
import android.os.Message;

/**
 * This class provides the information to show on the
 * {@link sesi.ejava.monitoring.MonitoringActivity} . This information is got
 * using the bluetooth connection managed by the {@link BluetoothManagerService}
 * .
 * 
 * @see sesi.ejava.monitoring.MonitoringActivity
 * @see BluetoothManagerService
 * 
 * @author Cesar Fuguet
 * 
 */
public class MonitoringInputStream {
	private byte temperature;
	private byte gear;
	private byte speed;
	private byte rpm;
	private byte motorOil;
	private byte gas;
	private byte[] byteStream;

	private static MonitoringInputStream instance = null;

	private MonitoringInputStream() {
		BluetoothManagerService bluetoothManagerService = BluetoothManagerService
				.getInstance();
		bluetoothManagerService.setHandler(handler);
	}

	/**
	 * This class is a singleton. The only way to get the singleton instance is
	 * using this function.
	 * 
	 * @return singleton instance
	 */
	public static synchronized MonitoringInputStream getInstance() {
		if (instance == null) {
			instance = new MonitoringInputStream();
		}

		return instance;
	}

	public byte getGear() {
		return gear;
	}

	public byte getTemperature() {
		return temperature;
	}

	@Deprecated
	public void setTemperature(byte temperature) {
		this.temperature = temperature;
	}

	public byte getSpeed() {
		return speed;
	}

	@Deprecated
	public void setSpeed(byte speed) {
		this.speed = speed;
	}

	public byte getRpm() {
		return rpm;
	}

	@Deprecated
	public void setRpm(byte rpm) {
		this.rpm = rpm;
	}

	public byte getMotorOil() {
		return motorOil;
	}

	@Deprecated
	public void setMotorOil(byte motorOil) {
		this.motorOil = motorOil;
	}

	public byte getGas() {
		return gas;
	}

	@Deprecated
	public void setGas(byte gas) {
		this.gas = gas;
	}

	public byte[] getByteStream() {
		return byteStream;
	}

	@Deprecated
	public void setByteStream(byte[] byteStream) {
		this.byteStream = byteStream;
	}

	/**
	 * This handler object gets the read informations from the
	 * BluetoothChatService
	 */
	private final Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case BluetoothManagerService.DATA_READ:
				byteStream = (byte[]) msg.obj;

				temperature = byteStream[0];
				speed = byteStream[1];
				rpm = byteStream[2];
				motorOil = byteStream[3];
				gas = byteStream[4];
				gear = byteStream[5];

				break;
			}
		}
	};
}
